SPB Git
1.9 KB · 23 lines typescript
1import { z } from "zod";2import { handler, parseBody, uuid, HttpError } from "@/lib/api";3import { createProperty, updateProperty, deleteProperty, reorderProperties, ensureDatabase } from "@/lib/databases/service";4import { PROPERTY_TYPES } from "@/lib/databases/types";5import { audit } from "@/lib/audit";6const type = z.enum(PROPERTY_TYPES); const config = z.record(z.string(), z.unknown());7export const POST = handler(async ({ ws, params, req, user }) => {8  const b = await parseBody(req, z.object({ name: z.string().max(200), type, config: config.optional(), width: z.number().int().min(40).max(2000).nullable().optional(), position: z.number().optional() }));9  const p = await createProperty(ws.id, params.id, b as any); await audit(user.id, "database.property.create", { type: "property", id: p.id }, { name: p.name, type: p.type });10  return Response.json(p, { status: 201 });11});12/** { id, ...patch } or { order: [ids] } */13export const PATCH = handler(async ({ ws, params, req }) => {14  const b = await parseBody(req, z.object({ id: uuid.optional(), name: z.string().max(200).optional(), type: type.optional(), config: config.optional(), width: z.number().int().min(40).max(2000).nullable().optional(), position: z.number().optional(), order: z.array(uuid).optional() }));15  if (b.order) { await reorderProperties(ws.id, params.id, b.order); return Response.json({ ok: true }); }16  if (!b.id) throw new HttpError(400, "id required"); const { id, order: _o, ...patch } = b;17  return Response.json(await updateProperty(ws.id, params.id, id, patch as any));18});19export const DELETE = handler(async ({ ws, params, req, user }) => {20  const id = uuid.parse(new URL(req.url).searchParams.get("id")); const d = await ensureDatabase(ws.id, params.id);21  await deleteProperty(ws.id, d.id, id); await audit(user.id, "database.property.delete", { type: "property", id }); return Response.json({ ok: true });22});23
Files · main
Menu

Theme